//
//  NewsViewController.m
//  e律师
//
//  Created by 韦淋 on 2017/5/23.
//  Copyright © 2017年 test. All rights reserved.
//

#import "NewsViewController.h"


@interface NewsViewController ()<LynnTypeButtonActionDelegate,UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *newsTableView;
@property (nonatomic, strong) NSMutableArray *firmInfo;
@property (nonatomic, strong) NSMutableArray *justiceBureauInfo;
@property (nonatomic, strong) NSMutableArray *systemInfo;

@property (nonatomic, assign) NSInteger typeFlag;
@property (nonatomic, assign) NSInteger newsFlag;
@property (nonatomic, assign) NSInteger product_brand;

@end

@implementation NewsViewController
- (void)viewWillAppear:(BOOL)animated{
    self.tabBarController.tabBar.hidden = YES;
    self.automaticallyAdjustsScrollViewInsets = false;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.barTitleLable.text = @"新闻资讯 ";
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.view.backgroundColor = [UIColor whiteColor];
    self.firmInfo = [NSMutableArray array];
    self.justiceBureauInfo = [NSMutableArray array];
    self.systemInfo = [NSMutableArray array];
    self.typeFlag = 0;
    self.newsFlag = 0;
    self.product_brand = 14;
    [self initUI];
    [self getInfo];
    
}
- (void)getInfo{
    

    [NetworkingManager getUrl:[NSString stringWithFormat:@"%@gsid=%@&product_brand=%ld",NEWS_URL,self.accepterId,self.product_brand] dic:nil Succeed:^(id responseData) {
        NSLog(@"dhuskahfjkah:%ld",self.product_brand);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        NSLog(@"----------%@",responseData);
        for (NSDictionary *dic  in responseData[@"result"]) {
            if ([dic[@"product_brand"] integerValue] == 14) {
                [self.firmInfo addObject:dic];
                if (self.firmInfo.count == 0) {
                    [MBProgressHUD showSuccess:@"暂无更多数据"];
                }
                self.newsFlag = 0;
            }
            if ([dic[@"product_brand"] integerValue] == 15) {
                [self.justiceBureauInfo addObject:dic];
                if (self.justiceBureauInfo.count == 0) {
                    [MBProgressHUD showSuccess:@"暂无更多数据"];
                }
                self.newsFlag = 1;
            }
            if ([dic[@"product_brand"] integerValue] == 16) {
                [self.systemInfo addObject:dic];
                if (self.systemInfo.count == 0) {
                    [MBProgressHUD showSuccess:@"暂无更多数据"];
                }
                self.newsFlag = 2;
            }
        }
        [self.newsTableView reloadData];
        [self.newsTableView.mj_header endRefreshing];
    } failure:^(NSError *error) {
        [MBProgressHUD showError:@"网络异常"];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}
- (void)loadNewData{
    [self.firmInfo removeAllObjects];
    [self.justiceBureauInfo removeAllObjects];
    [self.systemInfo removeAllObjects];
    [self getInfo];
}
- (void)initUI{
    LynnTypeButtonView  *typeBtnView = [[LynnTypeButtonView  alloc]initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, 44)];
    [typeBtnView setTypeButtonTitles:@[@"律所资讯",@"司法局资讯",@"系统资讯"] withDownLableHeight:2 andDeleagte:self];
    [typeBtnView setTypeButtonNormalColor:[UIColor blackColor] andSelectColor:[UIColor colorWithRed:102.0f/255.0f green:159.0f/255.0f blue:190.0f/255.0f alpha:1.0f]];
    typeBtnView.backgroundColor = [UIColor whiteColor];
    
    [self.view insertSubview:typeBtnView atIndex:0];
    [self.view insertSubview:self.newsTableView belowSubview:typeBtnView];
//    [self.view addSubview:typeBtnView];
//    [self.view addSubview:self.newsTableView];
}
#pragma mark - TypeButtonDelegate
//实现代理
-(void)didClickTypeButtonAction:(UIButton*)button withIndex:(NSInteger)index
{
    self.typeFlag = index;
    self.product_brand = index + 14;
    [self.firmInfo removeAllObjects];
    [self.justiceBureauInfo removeAllObjects];
    [self.systemInfo removeAllObjects];
    [self.newsTableView reloadData];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [self getInfo];
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 110;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.typeFlag == 0) {
        return self.firmInfo.count;
    }
    else if (self.typeFlag == 1){
        return self.justiceBureauInfo.count;
    }
    else{
        return self.systemInfo.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NewsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NewsCell" forIndexPath:indexPath];
    if (self.typeFlag == 0) {
        cell.titleLable.text = self.firmInfo[indexPath.row][@"product_name"];
        cell.publisherLable.text = [NSString stringWithFormat:@"发布人： %@",self.firmInfo[indexPath.row][@"insert_name"]];
        cell.releaseTimeLable.text = [NSString stringWithFormat:@"发布时间： %@",self.firmInfo[indexPath.row][@"insert_time"]];
    }
    if (self.typeFlag == 1) {
        cell.titleLable.text = self.justiceBureauInfo[indexPath.row][@"product_name"];
        cell.publisherLable.text = [NSString stringWithFormat:@"发布人： %@",self.justiceBureauInfo[indexPath.row][@"insert_name"]];
        cell.releaseTimeLable.text = [NSString stringWithFormat:@"发布时间： %@",self.justiceBureauInfo[indexPath.row][@"insert_time"]];
    }
    if (self.typeFlag == 2) {
        cell.titleLable.text = self.systemInfo[indexPath.row][@"product_name"];
        cell.publisherLable.text = [NSString stringWithFormat:@"发布人： %@",self.systemInfo[indexPath.row][@"insert_name"]];
        cell.releaseTimeLable.text = [NSString stringWithFormat:@"发布时间： %@",self.systemInfo[indexPath.row][@"insert_time"]];
    }
    
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NewsDetailViewController *NDVC= [[NewsDetailViewController alloc] init];
    if (self.typeFlag == 0) {
        NDVC.newsID = self.firmInfo[indexPath.row][@"id"];
    }
    if (self.typeFlag == 1) {
        NDVC.newsID = self.justiceBureauInfo[indexPath.row][@"id"];
    }
    if (self.typeFlag == 2) {
        NDVC.newsID = self.systemInfo[indexPath.row][@"id"];
    }
    [self.navigationController pushViewController:NDVC animated:YES];
}
#pragma mark - Getter

- (UITableView *)newsTableView{
    if (!_newsTableView) {
        _newsTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 120, self.view.bounds.size.width, self.view.bounds.size.height - 120) style:UITableViewStylePlain];
        _newsTableView.backgroundColor = [UIColor clearColor];
        _newsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _newsTableView.delegate = self;
        _newsTableView.dataSource = self;
        _newsTableView.estimatedRowHeight = 180;
        [_newsTableView registerClass:[NewsCell class] forCellReuseIdentifier:@"NewsCell"];
        _newsTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
        
    }
    return _newsTableView;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
